﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouErYuanModels;
using YouErYuanManage.Filter;
using YouErYuanBLL;
using System.Text;
using CommonLib;

namespace YouErYuanManage.Controllers
{
    [IsLogin]
    public class SchoolController : Controller
    {
        SchoolBLL schoolbll = new SchoolBLL();
        TeacherBLL teachbll = new TeacherBLL();
        TClassBLL tclassbll = new TClassBLL();

        //更新幼儿园简介和名称
        public string UpdateSchool()
        {
            string title = Request["title"];
            string content = Request["content"];
            //解码
            byte[] jmBytes = Convert.FromBase64String(content);
            string decodeContent = Encoding.UTF8.GetString(jmBytes);
            //string decodeContent = System.Web.HttpUtility.UrlDecode(content);

            Teacher user = HttpContext.Session["user"] as Teacher;
            School model = new School();
            model.Schoolid = user.Schoolid;
            model.Schoolname = title;
            //处理百度编辑器里面的图片
            string managerWWW = System.Configuration.ConfigurationManager.AppSettings["youeryuanAppManagerWWW"];
            if (decodeContent.Contains("/manager/ueditorImg"))
            {
                string imgAllHttpURL = managerWWW + "/manager/ueditorImg";
                decodeContent = decodeContent.Replace("/manager/ueditorImg", imgAllHttpURL);
            }
            model.Schoolintroduce = decodeContent;
            schoolbll.UpdateSchool(model);
            return CommonMethod.FormatJsonObj("1");
        }

        /// <summary>
        /// 查询校园简介
        /// </summary>
        /// <returns></returns>
        public string getSchoolInfo()
        {
            School model = new School();
            model.Schoolid = (HttpContext.Session["user"] as Teacher).Schoolid;
            var data = schoolbll.getSchoolInfo(model);
            return CommonMethod.FormatJsonObj("", data);
        }

        //分页，获取幼儿园列表
        [YouErYuanManage.Filter.IsLogin]
        public string GetSchoolList()
        {
            int pageIndex = 0;
            int pageSize = 0;
            try
            {
                pageIndex = Convert.ToInt32(Request["pageIndex"]);
                pageSize = Convert.ToInt32(Request["pageSize"]);
            }
            catch (Exception)
            {
                pageIndex = 1;
                pageSize = 10;
            }
            string name = Request["youeryuanName"];
            int total = 0;
            object dt = schoolbll.GetSchoolPageList(pageIndex, pageSize, name, out total);
            int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;//总页数
            var data = new
            {
                pageIndex = pageIndex,
                pageCount = pageCount,
                list = dt,
                total = total
            };
            return CommonMethod.FormatJsonObj("", data);
        }

        //添加幼儿园
        [YouErYuanManage.Filter.IsLogin]
        public string AddSchool()
        {
            string schoolName = Request["schoolName"];
            int count = schoolbll.AddSchool(schoolName);
            if (count == 1)
            {
                return CommonMethod.FormatJsonObj("1");
            }
            return CommonMethod.FormatJsonObj("2");
        }

        //幼儿园视频、签到截止日期设置，园长账户设置
        [YouErYuanManage.Filter.IsLogin]
        public string SettingSchool()
        {
            string schoolIds = Request["schoolIds"];
            string[] schoolIdArray = schoolIds.Split(',');
            string qvEndDate = Request["qvEndDate"];
            int count = schoolbll.SettingSchool(qvEndDate, schoolIdArray);
            if (count == schoolIdArray.Length)
            {
                return CommonMethod.FormatJsonObj(1);
            }
            else
            {
                return CommonMethod.FormatJsonObj(2);
            }
        }

        //园长账户设置
        [YouErYuanManage.Filter.IsLogin]
        public string SettingSchoolAccount()
        {
            string yzAccount = Request["yzAccount"];
            string pwd = Request["pwd"];
            string yzName = Request["yzName"];
            string schoolId = Request["schoolId"];
            string type = Request["type"];
            string status = Request["status"];//账户可用状态
            string teacherId = Request["teacherId"];
            int count = 0;
            //验证账号是否存在
            int ckcount = teachbll.CheckTeacherPhone(yzAccount);
            if (type.Equals("add"))//新增
            {
                if (ckcount == 0)
                {
                    count = schoolbll.SettingSchoolAccountAdd(schoolId, yzName, yzAccount, pwd, status);
                }
                else
                {
                    return CommonMethod.FormatJsonObj("9", "该账号已存在");
                }
            }
            else//修改
            {
                Teacher model = teachbll.GetTeacherById(teacherId);//查询教师原始信息
                if (model != null)
                {
                    if (model.Phone.Equals(yzAccount))//账号没改变
                    {
                        count = schoolbll.SettingSchoolAccountUpdate(teacherId, yzName, yzAccount, pwd, status);
                    }
                    else
                    {
                        if (ckcount == 0)
                        {
                            count = schoolbll.SettingSchoolAccountUpdate(teacherId, yzName, yzAccount, pwd, status);
                        }
                        else
                        {
                            return CommonMethod.FormatJsonObj("9", "该账号已存在");
                        }
                    }
                }
            }
            return CommonMethod.FormatJsonObj(count);
        }

        //验证教师账号是否存在
        public string CheckTeacherPhone()
        {
            string phone = Request["phone"];
            int count = teachbll.CheckTeacherPhone(phone);
            return CommonMethod.FormatJsonObj(count);
        }

        //批量设置签到消息模板
        public string UpdateSchoolQianDaoMuban()
        {
            string schoolIds = Request["schoolIds"];
            string[] arrids = schoolIds.Split(',');
            string xiaoxi = Request["xiaoxi"];
            int count = schoolbll.UpdateSchoolQianDaoMuBan(arrids, xiaoxi);
            if (count == arrids.Length)
            {
                return CommonMethod.FormatJsonObj(1);
            }
            return CommonMethod.FormatJsonObj(2);
        }

        //获取学校签到消息模板
        public string GetSchoolQianDaoMuban()
        {
            string schoolId = Request["schoolId"];
            School model = schoolbll.GetSchoolById(schoolId);
            string msg = "";
            if (model != null)
            {
                msg = model.QiaoDaoMuBan;
            }
            return CommonMethod.FormatJsonObj("", msg);
        }

        //获取学校下的所有班级
        public string GetSchoolBanJi()
        {
            string schoolId = Request["schoolId"];
            List<TClass> list = tclassbll.GetSchoolBanJi(schoolId);
            return CommonMethod.FormatJsonObj("", list);
        }

        //编辑学校名称
        public string EditSchoolName()
        {
            string name = Request["schoolName"];
            string schoolId = Request["schoolId"];
            int count = schoolbll.EditSchoolName(schoolId, name);
            return CommonMethod.FormatJsonObj(count);
        }


    }
}